package cn.edu.ntu.yyl.controller.ucenter;

import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.UserActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

import static cn.edu.ntu.yyl.tool.Md5Tool.code;

@Controller
public class UserAction extends HttpServlet {
    @Autowired
    UserActionService userActionService;
    @RequestMapping("/ucenter_WriteArticle.do")
    public String DoWriteArticle(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws Exception{
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        Article article = new Article();
        response.setContentType("text/html;charset=UTF-8");
        /*获取表单中的title*/
        String title = request.getParameter("article.title");
        /*获取表单中的content*/
        String content = request.getParameter("article.content");
        /*获取表单中的meta_keywords*/
        String meta_keywords = request.getParameter("article.meta_keywords");
        /*获取表单中的meta_description*/
        String meta_description = request.getParameter("article.meta_description");
        /*获取作者的Id*/
        Integer user_id = userInfo.getId();
        //写入数据
        article.setTitle(title);
        article.setContent(content);
        article.setMeta_keywords(meta_keywords);
        article.setMeta_descr(meta_description);
        article.setUser_id(user_id);
        article.setCreated(new Date());
        return null;
    }
    @RequestMapping("/ucenter_UpdateUserInfo.do")
    public String DoUpdateUserInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws Exception {
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        response.setContentType("text/html;charset=UTF-8");
        /*获取表单中的用户名*/
        String nickname = request.getParameter("user.nickname");
        /*获取表单中的邮箱*/
        String email = request.getParameter("user.email");
        /*获取表单中的真名*/
        String realname = request.getParameter("user.realname");
        //将数据写入临时UserInfo类user中
        userInfo = userActionService.getUserInfoByID(userInfo.getId());
        userInfo.setEmail(email);
        userInfo.setNickname(nickname);
        userInfo.setRealname(realname);
        //修改数据库
        userActionService.update(userInfo);
        if(userInfo.getId()>=1) {
            session.setAttribute("userInfo",userInfo);
            model.addAttribute("userInfo",session.getAttribute("userInfo"));
            return "ucenter/user_info";
        }
        else {
            return null;
        }
    }
    @RequestMapping("/ucenter_UpdateUserSignature.do")
    public String DoUpdateUserSignature(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws Exception{
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        response.setContentType("text/html;charset=UTF-8");
        /*获取表单中的签名*/
        String signature = request.getParameter("user.signature");
        //将数据写入临时UserInfo类user中
        userInfo.setSignature(signature);

        //修改数据库
        userActionService.update(userInfo);
        //修改内存
        userInfo = userActionService.getUserInfoByID(userInfo.getId());
        if(userInfo.getId()>=1) {
            session.setAttribute("userInfo",userInfo);
            model.addAttribute("userInfo",session.getAttribute("userInfo"));
            return "ucenter/signature";
        }
        else {
            return null;
        }
    }
    @RequestMapping("/ucenter_UpdateUserPassword.do")
    public String DoUpdateUserPassword(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws Exception{
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        response.setContentType("text/html;charset=UTF-8");
        /*获取表单内容*/
        String oldPwd = request.getParameter("oldPwd");
        String newPwd = request.getParameter("newPwd");
        String confirmPwd = request.getParameter("confirmPwd");
        //两次密码输入正确
        if (newPwd.equals(confirmPwd)){
            userInfo = userActionService.getUserInfoByID(userInfo.getId());
            if (userInfo.getPassword().equals(code(oldPwd))){
                //旧密码输入正确
                //修改密码
                userInfo.setPassword(code(newPwd));
                userActionService.update(userInfo);
            } else{
                //旧密码输入不正确
                response.getWriter().println("旧密码输入不正确！");
            }
        } else{
            //两次密码输入不一致
            response.getWriter().println("两次密码输入不一致！");
        }
        return "ucenter/pwd";

    }
    @RequestMapping("/ucenter_UpdateUserAvatar.do")
    public String DoUpdateUserAvatar(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        if (userInfo.getAvatar()==null|| userInfo.getAvatar().equals("")){
            userInfo.setAvatar(null);
        }
        return "ucenter/avatar";
    }
}
